using System;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Globalization;
using AjaxControlToolkit;
using System.Text.RegularExpressions;
using System.Web.Script.Serialization;
using System.Collections.Generic;

[assembly: System.Web.UI.WebResource("AjaxControlToolkitEx.DropDownPanelExtenderBehavior.js", "text/javascript")]

namespace AjaxControlToolkitEx
{
    [RequiredScript(typeof(CommonToolkitScripts))]
    [Designer(typeof(DropDownPanelExtenderDesigner))]
    //[ClientScriptResource("AjaxControlToolkitEx.DropDownPanelExtenderBehavior", "AjaxControlToolkitEx.DropDownPanelExtenderBehavior.js")]
    [ClientScriptResource("AjaxControlToolkitEx.DropDownPanelExtenderBehavior", "AjaxControlToolkitEx.DropDownPanelExtenderBehavior")]
    [TargetControlType(typeof(Control))]
    public class DropDownPanelExtender : ExtenderControlBaseEx
    {
        // TODO: Add your property accessors here.
        //


        [DefaultValue("")]
        [ExtenderControlProperty]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Justification = "Following ASP.NET AJAX pattern")]
        public string HiddenFieldID
        {
            get { return GetPropertyValue("HiddenFieldID", ""); }
            set { SetPropertyValue("HiddenFieldID", value); }
        }
        
        protected string HFID
        {
            get { return ID + "hf"; }
        }


        [DefaultValue("")]
        [ExtenderControlProperty]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Justification = "Following ASP.NET AJAX pattern")]
        public string ExtDataHiddenFieldID
        {
            get { return GetPropertyValue("ExtDataHiddenFieldID", ""); }
            set { SetPropertyValue("ExtDataHiddenFieldID", value); }
        }

        protected string ExtDataHFID
        {
            get { return ID + "hfExtData"; }
        }


        [ExtenderControlProperty]
        [DefaultValue("")]
        [IDReferenceProperty(typeof(Panel))]
        [RequiredProperty]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Justification = "Following ASP.NET AJAX pattern")]
        public string DropDownPanelID
        {
            get { return GetPropertyValue("DropDownPanelID", ""); }
            set { SetPropertyValue("DropDownPanelID", value); }
        }

        [ExtenderControlProperty]
        [DefaultValue("")]
        [IDReferenceProperty(typeof(WebControl))]
        [RequiredProperty]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Justification = "Following ASP.NET AJAX pattern")]
        public string DropDownButtonID
        {
            get { return GetPropertyValue("DropDownButtonID", ""); }
            set { SetPropertyValue("DropDownButtonID", value); }
        }

        [ExtenderControlProperty]
        [DefaultValue("")]
        [IDReferenceProperty(typeof(WebControl))]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Justification = "Following ASP.NET AJAX pattern")]
        public string CloseButtonID
        {
            get { return GetPropertyValue("CloseButtonID", ""); }
            set { SetPropertyValue("CloseButtonID", value); }
        }

        [DefaultValue(true)]
        [ExtenderControlProperty]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Justification = "Following ASP.NET AJAX pattern")]
        public bool CoverTargetControl
        {
            get { return GetPropertyValue("CoverTargetControl", false); }
            set { SetPropertyValue("CoverTargetControl", value); }
        }

        [DefaultValue(true)]
        [ExtenderControlProperty]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Justification = "Following ASP.NET AJAX pattern")]
        public bool ExcludeHangOffset
        {
            get { return GetPropertyValue("ExcludeHangOffset", false); }
            set { SetPropertyValue("ExcludeHangOffset", value); }
        }


        protected override void OnInit(EventArgs e)
        {
            CreateClientHiddenField();
            Page.PreLoad += new EventHandler(Page_PreLoad);
            base.OnInit(e);
        }

        void Page_PreLoad(object sender, EventArgs e)
        {
            if (((Page)sender).IsPostBack)
            {
                LoadClientHiddenFieldValue();
            }
        }
        
        private void CreateClientHiddenField()
        {
            {
                ExtenderControlBaseEx.HiddenFieldEx field = new ExtenderControlBaseEx.HiddenFieldEx();
                field.ID = HFID;
                Controls.Add(field);


                HiddenFieldID = GetClientID(HFID);
            }
            {
                ExtenderControlBaseEx.HiddenFieldEx field = new ExtenderControlBaseEx.HiddenFieldEx();
                field.ID = ExtDataHFID;
                Controls.Add(field);
                ExtDataHiddenFieldID = GetClientID(ExtDataHFID);
            }
        }
        private string Value = string.Empty;
        private string ExtDataValue = string.Empty;
        private void LoadClientHiddenFieldValue()
        {
            {
                ExtenderControlBaseEx.HiddenFieldEx hiddenField = (ExtenderControlBaseEx.HiddenFieldEx)NamingContainer.FindControl(HFID);
                if ((hiddenField != null) && !string.IsNullOrEmpty(hiddenField.Value))
                {
                    Value = hiddenField.Value;
                }
            }
            {
                ExtenderControlBaseEx.HiddenFieldEx hiddenField = (ExtenderControlBaseEx.HiddenFieldEx)NamingContainer.FindControl(ExtDataHFID);
                if ((hiddenField != null) && !string.IsNullOrEmpty(hiddenField.Value))
                {
                    ExtDataValue = hiddenField.Value;
                }
            }
        }
        protected void SaveClientClientHiddenFieldValue()
        {
            {
                ExtenderControlBaseEx.HiddenFieldEx hiddenField = (ExtenderControlBaseEx.HiddenFieldEx)NamingContainer.FindControl(HFID);
                if (hiddenField != null)
                {
                    hiddenField.Value = Value;
                }
            }
            {
                ExtenderControlBaseEx.HiddenFieldEx hiddenField = (ExtenderControlBaseEx.HiddenFieldEx)NamingContainer.FindControl(ExtDataHFID);
                if (hiddenField != null)
                {
                    hiddenField.Value = ExtDataValue;
                }
            }
        }
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            this.BehaviorID = GetClientID(ID) + "_behaviour";
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            SaveClientClientHiddenFieldValue();

            if (_operation != null)
            {
                string operation = _operation;
                if (ScriptManager.GetCurrent(Page).IsInAsyncPostBack)
                {
                    ScriptManager.GetCurrent(Page).RegisterDataItem(TargetControl, operation);
                }
                else
                {
                    string script = string.Format(CultureInfo.InvariantCulture, @"
                    (function() {{
                        var fn = function() {{
                            var behavior = $find('{0}');
                            if (behavior) {{
                                if ('{1}' == 'show'){{
                                    behavior.hide();
                                    behavior.show();
                                }}else
                                    behavior.hide();
                            }}
                            Sys.Application.remove_load(fn);
                        }};
                        Sys.Application.add_load(fn);
                    }})();", BehaviorID, operation);
                    ScriptManager.RegisterStartupScript(this, typeof(PopupPanelExtender), operation + BehaviorID, Helper.Trim(script), true);
                }
            }

            if (_closebtnClientID != null)
            {
                string script = string.Format(CultureInfo.InvariantCulture, @"
                (function() {{
                    var fn = function() {{
                        var behavior = $find('{0}');
                        if (behavior) {{
                            behavior.set_CloseButtonID('{1}');
                        }}
                        Sys.Application.remove_load(fn);
                    }};
                    Sys.Application.add_load(fn);
                }})();", BehaviorID, _closebtnClientID);
                ScriptManager.RegisterStartupScript(this, typeof(DropDownPanelExtender), "SetCloseButtonID" + BehaviorID, Helper.Trim(script), true);
            }

        }

        public bool PanelVisible
        {
            get
            {
                return Value == "1";
            }
        }

        private string _operation = null;

        public void Show()
        {
            string operation = "show";
            _operation = operation;
            Value = "1";
        }

        public void Hide()
        {
            string operation = "hide";
            _operation = operation;
            Value = "0";
        }

        private string _closebtnClientID = null;
        public void SetCloseButtonAtRuntime(string closebtnClientID)
        {
            _closebtnClientID = closebtnClientID;
        }

    }
}
